﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.TextManager.Interop;

namespace FixtureHighlighter
{
    class FixtureColorableItem : IVsColorableItem, IVsHiColorItem, IVsMergeableUIItem
    {
        public string Name
        {
            get;
            private set;
        }
        public string DisplayName
        {
            get;
            private set;
        }
        public COLORINDEX Foreground
        {
            get;
            private set;
        }
        public COLORINDEX Background
        {
            get;
            private set;
        }
        public Color HiForeground
        {
            get;
            private set;
        }
        public Color HiBackground
        {
            get;
            private set;
        }
        public uint FontFlags
        {
            get;
            private set;
        }

        public FixtureColorableItem(string name, string displayName, COLORINDEX foreground, Color hiForeground, COLORINDEX background, Color hiBackground, bool bold, bool strikethrough)
        {
            Name = name;
            DisplayName = displayName;
            Foreground = foreground;
            HiForeground = hiForeground;
            Background = background;
            HiBackground = hiBackground;
            FontFlags = (uint)FONTFLAGS.FF_DEFAULT;

            if (bold)
                FontFlags |= (uint)FONTFLAGS.FF_BOLD;
            if (strikethrough)
                FontFlags |= (uint)FONTFLAGS.FF_STRIKETHROUGH;
        }

        #region IVsColorableItem Members

        public int GetDefaultColors(COLORINDEX[] piForeground, COLORINDEX[] piBackground)
        {
            if (piForeground == null)
                throw new ArgumentNullException("piForeground");
            if (piForeground.Length == 0)
                throw new ArgumentOutOfRangeException("piForeground");

            if (piBackground == null)
                throw new ArgumentNullException("piBackground");
            if (piBackground.Length == 0)
                throw new ArgumentOutOfRangeException("piBackground");

            piForeground[0] = Foreground;
            piBackground[0] = Background;

            return VSConstants.S_OK;
        }

        public int GetDefaultFontFlags(out uint pdwFontFlags)
        {
            pdwFontFlags = FontFlags;
            return VSConstants.S_OK;
        }

        public int GetDisplayName(out string pbstrName)
        {
            pbstrName = DisplayName;
            return VSConstants.S_OK;
        }

        #endregion

        #region IVsHiColorItem Members

        public int GetColorData(int cdElement, out uint pcrColor)
        {
            pcrColor = 0;

            Color color = Color.Empty;
            switch ((__tagVSCOLORDATA)cdElement)
            {
                case __tagVSCOLORDATA.CD_BACKGROUND:
                    color = HiBackground;
                    break;
                case __tagVSCOLORDATA.CD_FOREGROUND:
                    color = HiForeground;
                    break;
                case __tagVSCOLORDATA.CD_LINECOLOR:
                    break;
                default:
                    break;
            }

            if (color.IsEmpty)
                return VSConstants.E_FAIL;

            pcrColor = (uint)ColorTranslator.ToWin32(color);
            return VSConstants.S_OK;
        }

        #endregion

        #region IVsMergeableUIItem Members

        public int GetCanonicalName(out string pbstrNonLocalizeName)
        {
            pbstrNonLocalizeName = Name;
            return VSConstants.S_OK;
        }

        public int GetDescription(out string pbstrDesc)
        {
            // Not implemented because the core text editor doesn't use it
            pbstrDesc = null;
            return VSConstants.E_NOTIMPL;
        }

        public int GetMergingPriority(out int piMergingPriority)
        {
            piMergingPriority = -1;
            return VSConstants.E_NOTIMPL;
        }

        #endregion
    }
}
